package com.rui1024.util;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;


public class JDBCUtil {

    private static String driver=null;
    private static String url=null;
    private static String user=null;
    private static String password=null;

    static {
        try {
            InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("personal.properties");
            Properties properties = new Properties();
            properties.load(in);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public Connection getCon() throws Exception {
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }

    public void closeCon(Connection con) throws SQLException {
        if(con!=null){
            con.close();
        }
    }

    @Test
    public  void test(){
        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            jdbcUtil.getCon();
            System.out.println("连接成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
